import COMPONENTS.MenuControl;
import COMPONENTS.twDropReason;
import COMPONENTS.twHelp;
import COMPONENTS.twSwitchTable;

import ENUMS.SlotStatus;

import EVENTS.CloseWithReasonEvent;
import EVENTS.SlotStatusChangedEvent;
import EVENTS.SwitchTableEvent;

import flash.events.Event;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.managers.PopUpManager;


public var QTData:Object;
public var kitchen_time:String = "";

public function set MenuFile(val:String):void{
	this.menucontrolMain.MenuFile = val;
}
public function set slotid(val:Number):void{
	this.menucontrolMain.slotID = val;
}

public function set cat(val:String):void{
	this.menucontrolMain.billCat = val;
}

public function set defaultServiceCharges(val:Boolean):void{
	this.menucontrolMain.defaultServiceCharges = val;
}

public function set server(val:String):void{
	this.menucontrolMain.server = val;
}

private var assign_title:String = "";

private function OnCC():void{
	try{
		this.setFocus();
		
		if ( this.QTData != null ){
			this.menucontrolMain.QTData = this.QTData.QTData;
			this.menucontrolMain.lastQTNumber = this.QTData.lastQTNumber;
			this.menucontrolMain.isResetQT = this.QTData.isResetQT;
		}
		
		this.menucontrolMain.addEventListener(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,OnCloseWithStatus);
		this.menucontrolMain.addEventListener(MenuControl.REPORT_ME,OnReportMe);
		this.assign_title = this.title;
		if ( this.QTData != null )
			if ( this.QTData.kitchen_time != null )
				this.kitchen_time = this.QTData.kitchen_time;
		
		this.title = this.assign_title + " " + this.kitchen_time;
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
public function UpdateTitle():void{
	try{
		if ( this.kitchen_time == "" ){
			var dtNow:Date = new Date;
			this.kitchen_time = "["+dtNow.toLocaleTimeString()+"]";
			this.title = this.assign_title + " " + this.kitchen_time; 
		} 
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private function OnReportMe(evt:Event):void{
	try{
		var sendData:Object = new Object();
		sendData.QTData = this.menucontrolMain.QTData;
		sendData.server = this.menucontrolMain.server;
		sendData.lastQTNumber = this.menucontrolMain.lastQTNumber;
		sendData.isResetQT = this.menucontrolMain.isResetQT;
		sendData.kitchen_time = this.kitchen_time;
		this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.REPORTING,sendData));
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private function OnCloseWithStatus(evt:SlotStatusChangedEvent):void{
	try{
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private function OnClose():void{
	try{
		if ( this.menucontrolMain.IsEmptyBill ){
			this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.FREE,null));
		}else{
			var sendData:Object = new Object();
			sendData.QTData = this.menucontrolMain.QTData;
			sendData.server = this.menucontrolMain.server;
			sendData.lastQTNumber = this.menucontrolMain.lastQTNumber;
			sendData.isResetQT = this.menucontrolMain.isResetQT;
			sendData.kitchen_time = this.kitchen_time;
			this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.PENDING,sendData));
		}
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private var twH:twHelp = null;

private function OnKeyUp(evt:KeyboardEvent):void{
	try{
		switch(evt.keyCode){
			case Keyboard.F1:
					this.twH = PopUpManager.createPopUp(this,twHelp,true) as twHelp;
					PopUpManager.centerPopUp(this.twH);
			break;
			case Keyboard.ESCAPE:
				this.OnClose();
			break;
		}
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
public function CustomerInfo():Object{
	return null;
}
private function OnDropBill():void{
	try{
		//Alert.show("Are you sure to drop the bill?","Drop bill confirmation",Alert.YES | Alert.NO,this,this.OnDropBillConfirmation);
		var dropres:twDropReason = PopUpManager.createPopUp(this,twDropReason,true) as twDropReason;
		dropres.addEventListener(CloseWithReasonEvent.DROP_WITH_REASON,OnDropBillConfirmation);
		PopUpManager.centerPopUp(dropres);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnDropBillConfirmation(evt:CloseWithReasonEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			this.BillDroped(evt.reason);
		}
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function BillDroped(dropReason:String):void{
	try{
		this.menucontrolMain.OnDropBill(dropReason);
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnResetKitchen():void{
	try{
		this.menucontrolMain.ResetKitchen();
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnSwitchTable():void{
	try{
		this.dispatchEvent(new SwitchTableEvent(SwitchTableEvent.ASK_FOR_AVAILABLE_SLOTS,""));
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
public function SetAvailableTables(parmArr:Array):void{
	try{
		var twAskForTable:twSwitchTable = PopUpManager.createPopUp(this,twSwitchTable,true) as twSwitchTable;
		twAskForTable.addEventListener(SwitchTableEvent.TABLE_SWITCHED,this.OnSwitchTableConfirmation);
		twAskForTable.dpTableOptions = parmArr;
		PopUpManager.centerPopUp(twAskForTable);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private function OnSwitchTableConfirmation(evt:SwitchTableEvent):void{
	try{
		var sendData:Object = new Object();
		sendData.QTData = this.menucontrolMain.QTData;
		sendData.server = this.menucontrolMain.server;
		sendData.lastQTNumber = this.menucontrolMain.lastQTNumber;
		sendData.isResetQT = this.menucontrolMain.isResetQT;
		sendData.kitchen_time = this.kitchen_time;
		sendData.newTable = evt.TableNo;
		this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.TABLE_SWITCH,sendData));
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}